package com.itaem.community.modules.infrastructure.company.company.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itaem.community.core.annotations.ResponseResult;
import com.itaem.community.core.entity.PageData;
import com.itaem.community.core.exception.common.IllegalParameterException;
import com.itaem.community.core.utils.PageUtils;
import com.itaem.community.modules.infrastructure.company.company.dto.CompanyCreateDTO;
import com.itaem.community.modules.infrastructure.company.company.dto.CompanyUpdateDTO;
import com.itaem.community.modules.infrastructure.company.company.mapper.CompanyTransformer;
import com.itaem.community.modules.infrastructure.company.company.service.PropertyManagementCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 物业管理公司 前端控制器
 * </p>
 *
 * @author tomato
 * @since 2020-10-28
 */
@Api(tags = "物业公司管理模块")
@ResponseResult
@RestController
@RequestMapping("/company")
public class PropertyManagementCompanyController {

    @Autowired
    private CompanyTransformer companyTransformer;

    @Autowired
    private PropertyManagementCompanyService propertyManagementCompanyService;

//    @RequiresRoles(value = {"超级管理员"}, logical = Logical.OR)
    @ApiOperation("分页查询物业公司信息")
    @GetMapping("/{page}/{size}")
    public PageData list(
            @ApiParam(value = "页码", example = "1") @PathVariable Long page,
            @ApiParam(value = "大小", example = "15") @PathVariable Long size) {
        return PageUtils
                .extract(propertyManagementCompanyService.page(new Page<>(page, size)))
                .map(companyTransformer::entityToBriefDto);
    }

//    @RequiresRoles(value = {"超级管理员"}, logical = Logical.OR)
    @ApiOperation("获取指定物业公司的详情")
    @GetMapping("/{id}")
    public Object info(@ApiParam("要查询的物业公司id") @PathVariable Integer id) {
        if (id <= 0) {
            throw new IllegalParameterException("物业公司id不可小于等于0!");
        }
        return propertyManagementCompanyService.findCompanyById(id);
    }

//    @RequiresRoles(value = {"超级管理员"}, logical = Logical.OR)
    @ApiOperation("注册物业公司")
    @PostMapping
    public Boolean save(@Valid @RequestBody CompanyCreateDTO dto) {
        return propertyManagementCompanyService.save(companyTransformer.createDtoToEntity(dto));
    }

//    @RequiresRoles(value = {"超级管理员"}, logical = Logical.OR)
    @ApiOperation("更新物业公司信息")
    @PatchMapping
    public Boolean update(@Valid @RequestBody CompanyUpdateDTO dto) {
        return propertyManagementCompanyService.updateById(companyTransformer.updateDtoToEntity(dto));
    }

//    @RequiresRoles(value = {"超级管理员"}, logical = Logical.OR)
    @ApiOperation("删除物业公司信息")
    @DeleteMapping("/{id}")
    public Boolean remove(@ApiParam(value = "要删除的物业公司id") @PathVariable Integer id) {
        return propertyManagementCompanyService.removeCompanyById(id);
    }
}

